package com.hcloud.common.crud.service.impl;

import com.hcloud.common.core.base.BaseBean;
import com.hcloud.common.crud.entity.BaseEntity;
import com.hcloud.common.crud.repository.BaseRepository;
import com.hcloud.common.crud.service.BaseDataService;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @Auther hepangui
 * @Date 2018/10/31
 */
public abstract class BaseDataServiceImpl<Entity extends BaseEntity,
        Repository extends BaseRepository<Entity>,
        Bean extends BaseBean>
        implements BaseDataService<Entity, Bean> {

    private Class<Entity> entityClass;
    private Class<Bean> beanClass;
    @Autowired
    protected Repository baseRepository;

    public BaseDataServiceImpl() {
        super();
        Class<?> c = getClass();
        //获取带有泛型参数的父类
        Type type = c.getGenericSuperclass();
        if (type instanceof ParameterizedType) {
            Type[] parameterizedType = ((ParameterizedType) type).getActualTypeArguments();
            entityClass = (Class<Entity>) parameterizedType[0];
            beanClass = (Class<Bean>) parameterizedType[2];
        }
    }

    @Override
    public Class<Entity> getEntityClass() {
        return entityClass;
    }

    @Override
    public Class<Bean> getBeanClass() {
        return beanClass;
    }

    @Override
    public Repository getRepository() {
        return baseRepository;
    }

}
